package com.axi.resplan.gui;


//Own imports
import com.axi.resplan.*;
import com.axi.ga.*;

//Common imports
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.event.*;
import java.awt.*;


/**
 * This is for displaying the whole timetable
 * @author Szabó Ákos - Axi
 *
 */
public class TimeTableFrame extends JFrame
{

	//variables
	private TimeTable _tt = new TimeTable();
	JSplitPane _hpane;
	JSplitPane _vpane;
	TaskTablePanel _ttp;
	TaskViewPanel _tvp;
	ResPlanGa _rpga = new ResPlanGa(_tt);
	GaEngine _gae = new GaEngine(_rpga, 0.005, 1000, 200, _rpga);
	TaskPlanPanel _tpp;
	
	/**
	 * Constructor
	 * @param argTitle
	 */
	public TimeTableFrame(String argTitle)
	{
		super(argTitle);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//frame.setLayout(new GridLayout(1, 1));
		setLayout(new BorderLayout());


		JMenu file = new JMenu("File");
		file.setMnemonic('F');

		JMenuItem newItem = new JMenuItem("New");
		newItem.setMnemonic('N');		
		file.add(newItem);
		newItem.addActionListener(
				new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						System.out.println("File/New is pressed");
						_ttp.loadData();
						_tvp.updateView();
					}
				}
			);


		JMenuItem bestItem = new JMenuItem("Find the BEST");
		bestItem.setMnemonic('B');
		file.add(bestItem);
		bestItem.addActionListener(
				new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						/*
						ResPlanDns d1 = new ResPlanDns(_tt);
						ResPlanDns d2 = new ResPlanDns(_tt);
						ResPlanDns d3 = (ResPlanDns)_rpga.crossover(d1, d2);
						System.out.println("d1 : "+d1);
						System.out.println("d2 : "+d2);
						System.out.println("d3 : "+d3);
						*/
						_gae.startLife();
						
					}
				}
			);

		file.add( "--------------" );

		JMenuItem exitItem = new JMenuItem("Exit");
		exitItem.setMnemonic('E');
		file.add(exitItem);
		exitItem.addActionListener(
				new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						System.out.println("Exit is pressed");
						System.exit( 1 );
					}
				}
			);
		
		JMenu ga = new JMenu("GA");
		file.setMnemonic('G');
		
		JMenuItem startGa = new JMenuItem("Start");
		startGa.setMnemonic('S');
		ga.add(startGa);
		startGa.addActionListener(
				new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						System.out.println("GA/START is pressed");
						_gae.startLife();
					}
				}
			);
		JMenuItem stopGa = new JMenuItem("Stop");
		stopGa.setMnemonic('T');
		ga.add(stopGa);
		stopGa.addActionListener(
				new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						System.out.println("GA/STOP is pressed");
						_gae.stopLife();
					}
				}
			);
		
		
		JMenuBar bar = new JMenuBar();
		setJMenuBar(bar);
		bar.add(file);
		bar.add(ga);

		
		//create the table panel for task list
		_ttp = new TaskTablePanel(_tt);
		//make it scrollable
		JScrollPane lsp = new JScrollPane(_ttp, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	
		
		
		//create the task view panel
		_tvp = new TaskViewPanel(_tt);
		//make it scrollable
		JScrollPane rsp = new JScrollPane(_tvp, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);	
		
		//synchronize scrolling
		lsp.getVerticalScrollBar().setModel(rsp.getVerticalScrollBar().getModel());
		
		
		//create the split between the table view and the graphival view
		_hpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, lsp, rsp);
		_hpane.setContinuousLayout(true);
		_hpane.setDividerLocation(400);		
		//add(_hpane, BorderLayout.CENTER);
		
		//add the TaskPlanPanel to the bottom as part as a split panel
		_tpp = new TaskPlanPanel(this);
		//create a bound scroll pane for the bottom plan JTable
		JScrollPane bsp = new JScrollPane(_tpp, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		
		//add the scrollable plan plane to the bottom and the vertical split to the top
		_vpane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, _hpane, bsp);
		_vpane.setDividerLocation(400);
		//add the vertical panel to the frame
		add(_vpane, BorderLayout.CENTER);
		
		
		//set size and make it visible
		setBounds(100,10,1000, 600);
		setVisible(true);
	}//end of constructor with title
	
	
	public void addTimeTable( TimeTable argTimeTable)
	{
		_tt = argTimeTable;
	}
	
	
	public TimeTable getTimeTable()
	{
		return _tt;
	}
	
	public GaEngine getGaEngine()
	{
		return _gae;
	}
	

}//end of class
